package com.jiuzhou.guanwang.jzcp.fragment;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.URLConfig;
import com.jiuzhou.guanwang.jzcp.adapter.JczqAdapter;
import com.jiuzhou.guanwang.jzcp.base.BaseFragment;
import com.jiuzhou.guanwang.jzcp.bean.MatchInfoBean;
import com.jiuzhou.guanwang.jzcp.bean.MatchInfoResult;
import com.jiuzhou.guanwang.jzcp.netutil.NetRequestUtil;
import com.jiuzhou.guanwang.jzcp.utils.CommonUtils;
import com.vise.log.ViseLog;

import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JczqFragment extends BaseFragment implements View.OnClickListener {
    private int currentIndex = 0;
    private TextView mAfterTv;
    private TextView mBeforeTv;
    private List<MatchInfoBean> mData;
    private ListView mJczqLv;
    private TextView mTimeTv;
    private JczqAdapter mZqAdapter;

    protected void initView(View view) {
        this.mBeforeTv = (TextView) view.findViewById(R.id.tv_before);
        this.mTimeTv = (TextView) view.findViewById(R.id.tv_time);
        this.mAfterTv = (TextView) view.findViewById(R.id.tv_after);
        this.mJczqLv = (ListView) view.findViewById(R.id.lv_jczq);
        this.mBeforeTv.setOnClickListener(this);
        this.mAfterTv.setOnClickListener(this);
    }

    protected int bindLayout() {
        return R.layout.fragment_jczq;
    }

    protected void initData() {

        Map<String, String> map = new HashMap<>();
        map.put("gameEn", "jczq");
        map.put("product", "caipiao_client");
        map.put("mobileType", "iphone");
        map.put("apiVer", "1.1");
        map.put("ver", "4.33");
        map.put("channel", "appstore");
        map.put("deviceId", "9D2FC859-2CA2-4EF3-96DC-5B7EE672869E");
        map.put("apiLevel", "27");
        Callback.Cancelable post = NetRequestUtil.getInstance().get(URLConfig.JCZQ, map, 101,
                new NetRequestUtil.NetResponseListener<String>() {
                    @Override
                    public void onSuccess(String response, int requestCode) {
                        ViseLog.d(response);
                        MatchInfoResult result = (MatchInfoResult) new Gson().fromJson(response, MatchInfoResult.class);
                        if (result.result == 100 && !CommonUtils.isEmpty(result.data) && result.data.get(0) != null && !CommonUtils.isEmpty(((MatchInfoBean) result.data.get(0)).matchInfo)) {
                            if (JczqFragment.this.mZqAdapter == null) {
                                JczqFragment.this.mZqAdapter = new JczqAdapter(JczqFragment.this.getActivity());
                                JczqFragment.this.mJczqLv.setAdapter(JczqFragment.this.mZqAdapter);
                            }
                            JczqFragment.this.mData = result.data;
                            JczqFragment.this.mZqAdapter.setData(((MatchInfoBean) result.data.get(JczqFragment.this.currentIndex)).matchInfo);
                            JczqFragment.this.setTextstatu();
                        }
                    }

                    @Override
                    public void onFailed(String response, int requestCode) {
                        ViseLog.e("请求失败");
                        showToast("返回失败");
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("返回解析异常");
                    }

                    @Override
                    public void onFinished() {
                    }
                });
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_after:
                if (this.mData != null) {
                    if (this.currentIndex > 0) {
                        this.currentIndex--;
                        this.mZqAdapter.setData(((MatchInfoBean) this.mData.get(this.currentIndex)).matchInfo);
                    }
                    setTextstatu();
                    return;
                }
                return;
            case R.id.tv_before:
                if (this.mData != null) {
                    if (this.currentIndex < this.mData.size() - 1) {
                        this.currentIndex++;
                        this.mZqAdapter.setData(((MatchInfoBean) this.mData.get(this.currentIndex)).matchInfo);
                    }
                    setTextstatu();
                    return;
                }
                return;
            default:
                return;
        }
    }

    private void setTextstatu() {
        this.mTimeTv.setText(((MatchInfoBean) this.mData.get(this.currentIndex)).period);
        if (this.currentIndex == 0) {
            this.mAfterTv.setTextColor(getResources().getColor(R.color.white_0_2));
        } else {
            this.mAfterTv.setTextColor(getResources().getColor(R.color.white_0_8));
        }
        if (this.currentIndex == this.mData.size() - 1) {
            this.mBeforeTv.setTextColor(getResources().getColor(R.color.white_0_2));
        } else {
            this.mBeforeTv.setTextColor(getResources().getColor(R.color.white_0_8));
        }
    }
}